package com.erp.stock.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.stock.dao.PrintingAndDyeingEnterpriseDao;
import com.erp.stock.entity.PrintingAndDyeingEnterprise;
import com.erp.stock.service.PrintingAndDyeingEnterpriseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PrintingAndDyeingEnterpriseServiceImpl implements PrintingAndDyeingEnterpriseService {

    @Autowired
    private PrintingAndDyeingEnterpriseDao dao;

    @Override
    public Boolean update(PrintingAndDyeingEnterprise printingAndDyeingEnterprise) {
        PrintingAndDyeingEnterprise printingAndDyeingEnterprise1 = dao.findById(printingAndDyeingEnterprise.getId()).get();
        BeanUtils.copyProperties(printingAndDyeingEnterprise,printingAndDyeingEnterprise1);
        return dao.saveAndFlush(printingAndDyeingEnterprise1) != null;
    }

    @Override
    public Boolean add(PrintingAndDyeingEnterprise printingAndDyeingEnterprise) {
        return dao.save(printingAndDyeingEnterprise)!=null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<PrintingAndDyeingEnterprise> listPage(int page, int size) {
        return dao.findAll(PageRequest.of(page, size)).getContent();
    }

    @Override
    public List<PrintingAndDyeingEnterprise> listPageLike(int page, int size, String column) {
        return dao.findAllByEnterpriseNameLike(StringUtil.addLike(column),PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id")));
    }

    @Override
    public List<PrintingAndDyeingEnterprise> list() {
        return dao.findAll();
    }
}
